﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using Inspinia_MVC5.Data;
using Inspinia_MVC5.Models;

namespace Inspinia_MVC5.Controllers
{
    public class EbooksController : Controller
    {
        private BooksSystemContext db = new BooksSystemContext();

        // GET: /Ebooks/
        public ActionResult Index()
        {
            var ebooks = db.Ebooks.Include(e => e.BookType);
            return View(ebooks.ToList());
        }

        // GET: /Ebooks/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Ebook ebook = db.Ebooks.Find(id);
            if (ebook == null)
            {
                return HttpNotFound();
            }
            return View(ebook);
        }

        // GET: /Ebooks/Create
        public ActionResult Create()
        {
            ViewBag.BookTypeId = new SelectList(db.BookTypes, "ID", "TypeName");
            return View();
        }

        // POST: /Ebooks/Create
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 https://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include="EbookName,EbookISBN,EbookPic,EbookAuthor,EbookPress,EbookPath,Status,BookTypeId,YueDuLiang,Desc")] Ebook ebook)
        {
            if (Request.Files["photo"]!=null&& Request.Files["pdf"] != null)
            {
                HttpPostedFileBase f = Request.Files["photo"];
                HttpPostedFileBase pdf = Request.Files["pdf"];
                if (f.ContentLength > 0)
                {
                    string finename = @"/Content/EBookImages/" + ebook.EbookISBN + f.FileName.Substring(f.FileName.LastIndexOf('.'));
                    f.SaveAs(Server.MapPath(finename));
                    ebook.EbookPic = finename;

                }
                if (pdf.ContentLength > 0)
                {
                    string finename = @"/pdf/" + ebook.EbookISBN + pdf.FileName.Substring(pdf.FileName.LastIndexOf('.'));
                    string path=ebook.EbookISBN+pdf.FileName.Substring(pdf.FileName.LastIndexOf('.'));
                    pdf.SaveAs(Server.MapPath(finename));
                    ebook.EbookPath = path;

                }
            }
            else
            {
                return View();

            }
            if (ebook.EbookName!=""&&ebook.EbookISBN!="")
            {
                
                db.Ebooks.Add(ebook);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.BookTypeId = new SelectList(db.BookTypes, "ID", "TypeName", ebook.BookTypeId);
            return View(ebook);
        }

        // GET: /Ebooks/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Ebook ebook = db.Ebooks.Find(id);
            if (ebook == null)
            {
                return HttpNotFound();
            }
            ViewBag.BookTypeId = new SelectList(db.BookTypes, "ID", "TypeName", ebook.BookTypeId);
            return View(ebook);
        }

        // POST: /Ebooks/Edit/5
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 https://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include="Id,EbookName,EbookISBN,EbookPic,EbookAuthor,EbookPress,EbookPath,Status,BookTypeId,YueDuLiang")] Ebook ebook)
        {
            if (ModelState.IsValid)
            {
                db.Entry(ebook).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.BookTypeId = new SelectList(db.BookTypes, "ID", "TypeName", ebook.BookTypeId);
            return View(ebook);
        }

        // GET: /Ebooks/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Ebook ebook = db.Ebooks.Find(id);
            if (ebook == null)
            {
                return HttpNotFound();
            }
            return View(ebook);
        }

        // POST: /Ebooks/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Ebook ebook = db.Ebooks.Find(id);
            db.Ebooks.Remove(ebook);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
